//
//  YWFunctionDBManager.m
//  maintain
//
//  Created by 烟雨云渊 on 17/1/5.
//  Copyright © 2017年 ewin. All rights reserved.
//

#import "YWFunctionDBManager.h"
#import "YWDBTool.h"

#import "YWFunctionModel.h"

@implementation YWFunctionDBManager
YWSingletonM(Manager)

- (FMDatabase*)fmdb{
    return [[YWDBTool shareDBTool] fmdb];
}

- (FMDatabaseQueue*)dbQeue{
    return [[YWDBTool shareDBTool] dbQeue];
}

NSString * const functionSql = @"CREATE TABLE IF NOT EXISTS 'function_menu' (functionId integer,functionName varchar,functionTag integer,offOrOn integer,imageString varchar);";

- (void)createFunctionMenuTable{
    [[self fmdb] executeUpdate:functionSql];
    NSMutableArray* blockMenus = self.defauleFunctionArray;
    [blockMenus enumerateObjectsUsingBlock:^(YWFunctionModel* _Nonnull function, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *sql = @"INSERT INTO 'function_menu' (functionId,functionName,offOrOn,functionTag,imageString) VALUES (?, ?, ?, ?, ?);";
        [[self fmdb] executeUpdate:sql,@(function.functionId),function.functionName,@(function.offOrOn),@(function.functionTag),function.imageString];
    }];
}

- (NSMutableArray *)defauleFunctionArray{
    NSMutableArray *defaultFunction = [[NSMutableArray alloc] init];
    NSArray *nameArr = @[@"设备台账",@"报障",@"巡更",@"巡查",@"检测",@"保养",@"维修",@"记录",@"清点",@"申领",@"抄表",@"签到",@"通知",@"临时任务",@"通讯录"];
    
    NSArray *imageArr = @[@"profile_equipmentLedger",@"profile_postTrouble",@"patroling",@"profile_patrol",@"profile_detection", @"profile_upkeep",@"profile_repair",@"profile_records",@"profile_checkRecord",@"profile_material",@"shuidianmei",@"attendance",@"profile_notice",@"profile_tempMission",@"profile_addressBook"];
    
    for (int i = 0; i<nameArr.count; i++) {
        YWFunctionModel * function = [[YWFunctionModel alloc] init];
        function.functionId = 10000+i;
        function.functionTag = 200 + i;
        function.functionName = nameArr[i];
        function.imageString = imageArr[i];
        function.offOrOn = YWFunctionStatusOn;
        [defaultFunction addObject:function];
    }
    return defaultFunction;
}

- (BOOL)saveFunctionWithArray:(NSArray *)functionArray{
    if (functionArray.count<=0) {
        return YES;
    }
    __block BOOL executeResult = YES;
    __block NSString* sql = nil;
    [self.dbQeue inTransaction:^(FMDatabase* db, BOOL* rollback) {
        [functionArray enumerateObjectsUsingBlock:^(YWFunctionModel* _Nonnull function, NSUInteger idx, BOOL* _Nonnull stop) {
            sql = @"SELECT  * FROM 'function_menu' WHERE functionId=?";
            FMResultSet* set = [db executeQuery:sql, @(function.functionId)];
            if ([set next]) {
                sql = @"UPDATE 'function_menu' SET functionName=?, offOrOn=?, functionTag=?, imageString =? WHERE functionId = ?;";
                executeResult = [db executeUpdate:sql,function.functionName,@(function.offOrOn),@(function.functionTag),function.imageString,@(function.functionId)];
            }
            else {
                sql = @"INSERT INTO 'function_menu' (functionId,functionName,offOrOn,functionTag,imageString) VALUES (?, ?, ?, ?, ?);";
                executeResult = [db executeUpdate:sql,@(function.functionId),function.functionName,@(function.offOrOn),@(function.functionTag),function.imageString];
            }
            if (!executeResult) {
                *stop = YES;
                *rollback = YES;
            };
        }];
        [db closeOpenResultSets];
    }];
    return executeResult;
}

- (BOOL)updateFunctionStatus:(NSInteger)offOrOn withFunctionId:(NSInteger)functionId{
    __block BOOL executeResult = YES;
    __block NSString* sql = nil;
    [self.dbQeue inTransaction:^(FMDatabase* db, BOOL* rollback) {
        sql = @"SELECT * FROM 'function_menu' WHERE functionId=?;";
        FMResultSet* set = [db executeQuery:sql, @(functionId)];
        if ([set next]) {
            sql = @"UPDATE 'function_menu' SET offOrOn =? WHERE functionId =?;";
            executeResult = [db executeUpdate:sql, @(offOrOn), @(functionId)];
        }
        if (!executeResult) {
            *rollback = YES;
        };
        
        [db closeOpenResultSets];
    }];
    return executeResult;
}

- (NSArray*)getFunctionMenuWithOffOrOn:(NSInteger)offOrOn isSetting:(BOOL)setting{
    __block NSMutableArray* functionArray = [NSMutableArray array];
    [self.dbQeue inDatabase:^(FMDatabase* db) {
        NSString* sql = nil;
        FMResultSet* set = nil;
        if (setting) {
            sql = @"SELECT * FROM 'function_menu' ORDER BY functionTag ASC";
            set = [db executeQuery:sql];
        }else{
            sql = @"SELECT * FROM 'function_menu' WHERE offOrOn =? ORDER BY functionTag ASC";
            set = [db executeQuery:sql,@(offOrOn)];
        }
        while ([set next]) {
            YWFunctionModel* function = [self functionWithFMResultSet:set FMDatabase:db];
            [functionArray addObject:function];
        }
        [db closeOpenResultSets];
    }];
    return functionArray.count ? functionArray : nil;
}

- (YWFunctionModel*)functionWithFMResultSet:(FMResultSet*)set FMDatabase:(FMDatabase*)db{
    YWFunctionModel *function = [[YWFunctionModel alloc] init];
    function.functionId = [set intForColumn:@"functionId"];
    function.functionTag = [set intForColumn:@"functionTag"];
    function.functionName = [set stringForColumn:@"functionName"];
    function.offOrOn = [set intForColumn:@"offOrOn"];
    function.imageString = [set stringForColumn:@"imageString"];
    return function;
}

- (YWFunctionModel*)getFunctionModelWithFunctionModelId:(NSInteger)functionId{
    __block YWFunctionModel* function = nil;
    [self.dbQeue inDatabase:^(FMDatabase* db) {
        NSString* sql = @"SELECT * FROM 'function_menu' WHERE functionId = ? ;";
        FMResultSet* set = [db executeQuery:sql, @(functionId)];
        if ([set next]) {
            function = [self functionWithFMResultSet:set FMDatabase:db];
        }
        [db closeOpenResultSets];
    }];
    return function;
}

- (NSArray*)getFunctionArrayWithFunctionIdArray:(NSArray*)functionIdArray{
    if (functionIdArray.count<=0) {
        return nil;
    }
    __block NSMutableArray * functionArray = [NSMutableArray array];
    [self.dbQeue inDatabase:^(FMDatabase* db) {
        [functionIdArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSInteger functionId = [obj integerValue];
            NSString* sql = @"SELECT * FROM 'function_menu' WHERE functionId =?";
            FMResultSet* set = [db executeQuery:sql, @(functionId)];
            while ([set next]) {
                YWFunctionModel* function = [self functionWithFMResultSet:set FMDatabase:db];
                [functionArray addObject:function];
            }
        }];
        [db closeOpenResultSets];
    }];
    return functionArray.count >0 ? functionArray:nil;
}

@end
